using System;
using System.Data;

using System.Configuration;

using System.Data.OleDb;
using Entity;

namespace SalesVMT
{
    public class ItemDB
    {
        #region PUBLIC
        public static DataSet GetAll()
        {
            DataSet retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Item_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetOne(int _Item_ID)
        {
            DataSet retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Item_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Item_ID", _Item_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetList(out int ItemCount, string keyWord, int currentPage, int pageSize)
        {

            DataSet retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Item_GetList", dbConn);

            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
        public static bool Delete(int _Item_ID)
        {
            bool retVal = false;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Item_Delete", dbConn);
            dbCmd.Parameters.AddWithValue("@Item_ID", _Item_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Insert(ItemInfo _ItemInfo)
        {
            int retVal = 0;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Item_Insert", dbConn);
            dbCmd.Parameters.AddWithValue("@Item_Code", _ItemInfo.Item_Code);
            dbCmd.Parameters.AddWithValue("@Item_Name", _ItemInfo.Item_Name);
            dbCmd.Parameters.AddWithValue("@Item_OriginID", _ItemInfo.Item_OriginID);
            dbCmd.Parameters.AddWithValue("@Item_CalculationUnitID", _ItemInfo.Item_CalculationUnitID);
            dbCmd.Parameters.AddWithValue("@Item_PurchasePrice", _ItemInfo.Item_PurchasePrice);
            dbCmd.Parameters.AddWithValue("@Item_WholesalePrice", _ItemInfo.Item_WholesalePrice);
            dbCmd.Parameters.AddWithValue("@Item_RetailPrice", _ItemInfo.Item_RetailPrice);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Update(ItemInfo _ItemInfo)
        {
            bool retVal = false;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Item_Update", dbConn);

            dbCmd.Parameters.AddWithValue("@Item_ID", _ItemInfo.Item_ID);
            dbCmd.Parameters.AddWithValue("@Item_Code", _ItemInfo.Item_Code);
            dbCmd.Parameters.AddWithValue("@Item_Name", _ItemInfo.Item_Name);
            dbCmd.Parameters.AddWithValue("@Item_OriginID", _ItemInfo.Item_OriginID);
            dbCmd.Parameters.AddWithValue("@Item_CalculationUnitID", _ItemInfo.Item_CalculationUnitID);
            dbCmd.Parameters.AddWithValue("@Item_PurchasePrice", _ItemInfo.Item_PurchasePrice);
            dbCmd.Parameters.AddWithValue("@Item_WholesalePrice", _ItemInfo.Item_WholesalePrice);
            dbCmd.Parameters.AddWithValue("@Item_RetailPrice", _ItemInfo.Item_RetailPrice);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static ItemInfo GetInfo(int _Item_ID)
        {
            ItemInfo retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Item_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Item_ID", _Item_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            OleDbDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new ItemInfo();
                    retVal.Item_ID = Convert.ToInt32(dr["Item_ID"]);
                    retVal.Item_Code = Convert.ToString(dr["Item_Code"]);
                    retVal.Item_Name = Convert.ToString(dr["Item_Name"]);
                    retVal.Item_OriginID = Convert.ToInt32(dr["Item_OriginID"]);
                    retVal.Item_CalculationUnitID = Convert.ToInt32(dr["Item_CalculationUnitID"]);
                    retVal.Item_PurchasePrice = Convert.ToDecimal(dr["Item_PurchasePrice"]);
                    retVal.Item_WholesalePrice = Convert.ToDecimal(dr["Item_WholesalePrice"]);
                    retVal.Item_RetailPrice = Convert.ToDecimal(dr["Item_RetailPrice"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public static string GetMaxCode()
        {
            string retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Item_GetMaxCode", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToString(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Using(int ItemID)
        {
            int retVal = 0;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Item_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Item_ID", ItemID);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Exist(int ItemID, string ItemName)
        {
            bool retVal = false;
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Item_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Item_Name", ItemName);
            dbCmd.Parameters.AddWithValue("@Item_ID", ItemID);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        #endregion
    }
}
